package com.jsyl.lwbk.web.controller.business;

import com.jsyl.lwbk.business.domain.EvaluateReport;
import com.jsyl.lwbk.business.mapper.EvaluateReportMapper;
import com.jsyl.lwbk.business.service.IChildQuestionService;
import com.jsyl.lwbk.business.vo.ChildQuestionVO;
import com.jsyl.lwbk.common.core.controller.BaseController;
import com.jsyl.lwbk.common.core.domain.AjaxResult;
import com.jsyl.lwbk.common.utils.DateUtils;
import com.jsyl.lwbk.common.utils.StringUtils;
import com.jsyl.lwbk.framework.util.ShiroUtils;
import com.jsyl.lwbk.system.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Api(value = "小孩出题")
@Controller
@RequestMapping("/business/childQuestion")
public class ChildQuestionController extends BaseController {

    private String prefix = "business/childEvaluate";

    @Autowired
    IChildQuestionService childQuestionService;
    @Autowired
    EvaluateReportMapper evaluateReportMapper;

    @ApiOperation(value = "获取小孩问题")
    @ApiImplicitParams({@ApiImplicitParam(name = "childId", value = "小孩id")})
//    @RequiresPermissions("business:evaluateFlag:view")
    @GetMapping("/getChildQuestion/{childId}")
    public String getChildQuestion(@PathVariable("childId") Long childId, String evaluateNumber, ModelMap mmap) {
        //20210104 石崖姐目前要求，同时只存在一份测评项目，如果未超过30天，就继续整
        if (StringUtils.isEmpty(evaluateNumber)) {
            EvaluateReport evaluateReport = evaluateReportMapper.findLastEvaluateReport(childId);
            if (evaluateReport != null && evaluateReport.getStatus() == 1 && DateUtils.dayCompareDay(evaluateReport.getCreateTime(), new Date()) <= 30) {
                evaluateNumber = evaluateReport.getEvaluateNumber();
            }
        }
        List<ChildQuestionVO> list = childQuestionService.getChildQuestion(childId, evaluateNumber);
        mmap.put("childId", childId);
        mmap.put("evaluateNumber", evaluateNumber);
        mmap.put("childQuestion", list);
        return prefix + "/childQuestion";
    }
    @ApiOperation(value = "学习评估")
    @ApiImplicitParams({@ApiImplicitParam(name = "childId", value = "小孩id")})
    @RequiresPermissions("business:evaluateFlag:view")
    @GetMapping("/learningEvaluate/{childId}")
    public String learningEvaluate(@PathVariable("childId") Long childId,ModelMap mmap) {
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("childId", childId);
        mmap.put("token", user.getToken());
        return prefix + "/learningEvaluate";
    }
    @ApiOperation(value = "修改学习评估")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "childId", value = "小孩id"),
            @ApiImplicitParam(name = "reportId", value = "报告ID")
    })
    @GetMapping("/editLearningEvaluate/{childId}/{reportId}")
    public String editLearningEvaluate(@PathVariable(value = "childId") Long childId,@PathVariable(value = "reportId") Long reportId,ModelMap mmap) {
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("childId", childId);
        mmap.put("reportId", reportId);
        mmap.put("token", user.getToken());
        return prefix + "/learningEvaluate";
    }

    /**
     * 检查老师在该中心是否有测评证书
     * @param centerId
     * @return
     */
    @ApiOperation(value = "检查老师在该中心是否有测评证书")
    @ApiImplicitParams({@ApiImplicitParam(name = "centerId", value = "中心id")})
    @ResponseBody
    @GetMapping("/checkCertificate/{centerId}")
    public AjaxResult checkCertificate(@PathVariable("centerId") String centerId) {
//        SysUser user = ShiroUtils.getSysUser();
//        List<String> ignoreList = Lists.newArrayList("ADMINISTRATOR","JW","TR","PM","HCS");
//        if(!org.springframework.util.CollectionUtils.isEmpty(user.getRoles())){
//            for(SysRole role : user.getRoles()){
//                if(ignoreList.contains(role.getRoleKey().toUpperCase())){
//                    return AjaxResult.success("操作成功", "true");
//                }
//            }
//        }
//        if(CollectionUtils.isNotEmpty(user.getVipCertList())){
//            for(Map m:user.getVipCertList()){
//                if(centerId.equals(m.get("centerId").toString())){
//                    return m.get("status").toString().equals("1")?AjaxResult.success("操作成功", "true"):AjaxResult.success("操作成功", "false");
//                }
//            }
//        }
//        return AjaxResult.success("操作成功", "false");
        //等jms上线
        return AjaxResult.success("操作成功", "true");
    }

}
